package com.sskj.qiangjin.ui.fragment;


import android.os.Build;
import android.support.annotation.RequiresApi;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;

import com.afollestad.materialdialogs.internal.ThemeSingleton;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.sskj.common.adapter.slimadapter.IViewInjector;
import com.sskj.common.adapter.slimadapter.SlimAdapter;
import com.sskj.common.adapter.slimadapter.SlimInjector;
import com.sskj.common.base.App;
import com.sskj.common.rxbus2.RxBus;
import com.sskj.common.rxbus2.Subscribe;
import com.sskj.common.rxbus2.ThreadMode;
import com.sskj.common.util.NumberUtil;
import com.sskj.lib.BaseHttpConfig;
import com.sskj.lib.RConfig;
import com.sskj.lib.base.BaseFragment;
import com.sskj.qiangjin.R;
import com.sskj.qiangjin.bean.StockBloomBean;
import com.sskj.qiangjin.presenter.AllGuessHistoryFragmentPresenter;
import com.sskj.qiangjin.util.HideUtil;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import io.reactivex.Flowable;

@Route(path = RConfig.APP_FRAGMENT_ALL_GUESS)//所有竞猜
public class AllGuessHistoryFragment extends BaseFragment<AllGuessHistoryFragmentPresenter> {
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    private SlimAdapter slimAdapter;


    @Override
    protected int getLayoutId() {
        return R.layout.app_fragment_all_guess_history;
    }

    @Override
    public AllGuessHistoryFragmentPresenter getPresenter() {
        return new AllGuessHistoryFragmentPresenter();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void changeFive(StockBloomBean stockBloomBean) {
        if (stockBloomBean != null) {
            List<StockBloomBean.DataBean.XiazhuBeans> xiazhu_list = stockBloomBean.getData().get(1).getXiazhu_list();
            slimAdapter.updateData(xiazhu_list);
        }
    }

    @Override
    public void initView() {
        RxBus.getDefault().register(this);
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        slimAdapter = SlimAdapter.create().register(R.layout.app_recy_item_all_guess_history, new SlimInjector<StockBloomBean.DataBean.XiazhuBeans>() {
            @RequiresApi(api = Build.VERSION_CODES.M)
            @Override
            public void onInject(StockBloomBean.DataBean.XiazhuBeans data, IViewInjector injector, List payloads) {
                if (slimAdapter.getData().indexOf(data) % 2 == 0) {
                    injector.background(R.id.llBg, ContextCompat.getDrawable(App.INSTANCE, R.drawable.app_bg_7d7));
                } else {
                    injector.backgroundColor(R.id.llBg, ContextCompat.getColor(App.INSTANCE, R.color.app_696));
                }
                String palyerName = data.getRealname();
                if (HideUtil.isInteger(palyerName)) {
                    if (palyerName.length() == 11) {
                        palyerName = HideUtil.getPhoneHide(palyerName);
                    }
                }
                if (TextUtils.isEmpty(data.getUpic())) {
                    injector.image(R.id.iv_header, App.INSTANCE.getResources().getDrawable(R.mipmap.app_icon_wolf));
                } else {
                    injector.imageCircle(R.id.iv_header, BaseHttpConfig.BASE_URL + data.getUpic());
                }
                String moneyType = data.getPid() == 1 ? "USDT" : data.getPid() == 2 ? "QC" : "金币";
                injector.text(R.id.tv_player, palyerName)
                        .text(R.id.tv_put, data.getBuy_money() + moneyType);
                int status = data.getStatus();
                switch (status) {
                    case 1:
                        injector.text(R.id.tv_run, "--")
                                .text(R.id.tv_income, "--")
                                .textColor(R.id.tv_run, ContextCompat.getColor(getActivity(), R.color.commonWhite))
                                .textColor(R.id.tv_income, ContextCompat.getColor(getActivity(), R.color.commonWhite));
                        break;
                    case 2:
                        BigDecimal bet = new BigDecimal(data.getFinal_beishu());
                        BigDecimal money = new BigDecimal(data.getBuy_money());
                        injector.text(R.id.tv_run, data.getFinal_beishu() + "倍")
                                .text(R.id.tv_income, NumberUtil.keep4(bet.multiply(money).toString()) + moneyType)
                                .textColor(R.id.tv_run, ContextCompat.getColor(getActivity(), R.color.app_80e))
                                .textColor(R.id.tv_income, ContextCompat.getColor(getActivity(), R.color.app_80e));
                        break;
                    case 3:
                        injector.text(R.id.tv_run, data.getFinal_beishu() + "倍")
                                .text(R.id.tv_income, "-" + data.getBuy_money() + moneyType)
                                .textColor(R.id.tv_run, ContextCompat.getColor(getActivity(), R.color.app_e64))
                                .textColor(R.id.tv_income, ContextCompat.getColor(getActivity(), R.color.app_e64));
                        break;
                }
            }
        }).attachTo(recyclerView).updateData(new ArrayList());
//        Flowable.rangeLong(0, 8)
//                .map(i -> i + "")
//                .toList()
//                .subscribe(strings -> slimAdapter.updateData(strings));
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        RxBus.getDefault().unregister(this);
    }
}
